!function ($) {

  "use strict"; // jshint ;_;

  $.fn.accsetting = function ( option ) {
	
	var setting = $.extend({
		rowClick: function(){
			return true;
		},
		rowExpand: function(){
			return true;
		}
	},option);  
	  
    return this.each(function () {
      var $lswraprow = new Array();	
      var $focus = null, i = 0;
      
      $(this).find('.accrow').each(function(){
    	  var $accrow = $(this);
    	  var $accbody = $accrow.find('.accbody');
    	  var $accedit = $accrow.find('.accedit');
    	  var $accaction = $accrow.find('.accaction');
    	  var $accicon = $accaction.find('.accicon'); 
    	  var $statussuccess = $accrow.find('.accstatus-success');
    	  var $statusfail = $accrow.find('.accstatus-fail');
    	  
    	  var $wrapedit = $.extend($accedit, {
    		  accfields: $accedit.find('.accfield')
    		  ,
    		  init: function(){
    			  debug("running init of accedit");
    			  this.accfields.each(function(){
    				  $(this).data('init',$(this).val());
    			  });
    		  },
    		  updateData: function(){
    			  this.accfields.each(function(){
    				  if($(this).attr('type')!='password'){
    					  $(this).data('init',$(this).val());
    				  }
    			  });
    		  },
    		  reset: function(){
    			  this.accfields.each(function(){
     				 debug("accfields.datainit=" + $(this).data('init'));
     				 $(this).val($(this).data('init')); 
    			  });
    			  $statusfail.removeClass('show');
    			  $statusfail.html('');
    		  }
    	  });
    	  
    	  var $wraprow = $.extend($accrow, {
    		  id: i++,
    		  edited: false,
    		  accbody: $accbody,
    		  accedit: $wrapedit,
    		  accaction: $accaction,
    		  accicon: $accicon,
    		  statussuccess: $statussuccess,
    		  statusfail: $statusfail,
    		  init: function(){
    			  this.accedit.init();
    		  },
    		  reset: function(){
    			  debug("running reset of accrow");
    			  this.edited = false;
    			  this.accedit.reset();
    			  this.removeClass('accrowedited');
				  this.statussuccess.removeClass('show');
    			  this.close();
    		  },
    		  toggle: function(){
    			  debug('calling toggle for row ' + this.id);
    			  debug('toggle is: ' + $accrow.data('toggle'));
    			  if($accrow.data('toggle')=='on'){
    				  return;
    			  }else{
    				  if($focus){
    					  $focus.close();
    				  }
    				  $focus = this;
    				  this.expand();
    			  }
    		  },
    		  close: function(){
    			  debug('calling close for row ' + this.id);
    			  this.accedit.hide();
    			  this.accedit.reset();
    			  
    			  this.accbody.show();
    			  if(this.edited){
    				  this.addClass('accrowedited');
    				  this.statussuccess.addClass('show');
    			  }
    			  $accrow.data('toggle','off');
    			  $accrow.removeClass('accrowfocus');
    		  },
    		  expand: function(){
    			  debug('calling expand for row ' + this.id);
    			  $wraprow.addClass('accloading');
    			  $.when(setting.rowExpand($accrow)).done(function(data, textStatus, jqXHR){
    				  
    				  $wraprow.accedit.show();
    				  $wraprow.accbody.hide();
        			  if($wraprow.edited){
        				  $wraprow.removeClass('accrowedited');
        				  $wraprow.statussuccess.removeClass('show');
        			  }
        			  $accrow.data('toggle','on');
        			  $accrow.addClass('accrowfocus');
        			  debug('setting toggle to on');
    				  $wraprow.removeClass('accloading');
    				  
				  }).fail(function(jqXHR, textStatus, errorThrown){
					  $wraprow.removeClass('accloading');
				  });
    		  },
    		  changeStatus: function(status,msg){
    			  debug("set in active row status: "+status);
    			  
    			  if(status){
    				  this.edited = true;
        			  this.accedit.updateData();
        			  this.addClass('accrowedited');
        			  this.close();
    				  this.statusfail.removeClass('show');
    				  this.statussuccess.addClass('show');
    				  if(msg){
    					  this.statussuccess.html(msg);
    				  }
    			  }else{
    				  this.statussuccess.removeClass('show');
    				  this.statusfail.addClass('show');
    				  if(msg){
    					  this.statusfail.html(msg);
    				  }
    			  }
    		  }
    	  });
    	  
    	  $wraprow.init();
    	  
    	  $wraprow.click(function(){
    		  if(setting.rowClick && (typeof setting.rowClick == 'function')){
    			  $wraprow.addClass('accloading');
    			  $.when(setting.rowClick($accrow)).done(function(data, textStatus, jqXHR){
    				  $wraprow.toggle();
    				  $wraprow.removeClass('accloading');
				  }).fail(function(jqXHR, textStatus, errorThrown){
					  $wraprow.removeClass('accloading');
				  });
    		  }else{
    			  $wraprow.toggle();  
    		  }
          });
    	  
    	  $wraprow.find('.btn-close').click(function(e){
    		  e.stopPropagation();
    		  debug("button close is clicked");
    		  $wraprow.statusfail.removeClass('show');
    		  $wraprow.statusfail.html('');
    		  $wraprow.close();
    	  });
    	  
    	  $lswraprow.push($wraprow);
      });
      
      /*public method*/
      
      this.getActiveRow = function(){
    	  debug("get active row");
    	  return $focus;
      };
      
      this.reset = function(){
    	  debug("form hidden");
    	  for(var i=0; i < $lswraprow.length; i++){
    		  $lswraprow[i].reset();
    	  }
      };
      
    });
  }


}(window.jQuery);
